import './public-path'
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import { routes } from './router'
import store from './store'

Vue.config.productionTip = false

let instance: any = null
let router: any = null
const isProduction = process.env.NODE_ENV === 'production'

function render (props: any = {}) {
  const { container } = props
  router = new VueRouter({
    routes,
    base: window.__POWERED_BY_QIANKUN__ ? '/vue-app/' : isProduction ? '/vue/' : '/',
    mode: 'history'
  })
  instance = new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount(container ? container.querySelector('#app') : '#app')
}

if (!window.__POWERED_BY_QIANKUN__) {
  render()
}
export async function bootstrap () {
  console.log('[vue] vue app bootstraped')
}
export async function mount (props: any) {
  console.log('[vue] props from main framework', props)
  props.onGlobalStateChange((state: any, prev: any) => {
    // state: 变更后的状态; prev 变更前的状态
    console.log(state, prev)
  })
  render(props)
}
export async function unmount () {
  instance.$destroy()
  instance.$el.innerHTML = ''
  instance = null
  router = null
}
